package com.haoshuang.zhouzhoubang.ui.page.order.take.services

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityChooseServicesBinding
import com.haoshuang.zhouzhoubang.model.bean.ChooseServiceBean
import com.haoshuang.zhouzhoubang.ui.page.order.take.services.detail.ChooseServiceDetailActivity
import com.haoshuang.zhouzhoubang.ui.page.order.take.services.detail.meal.ServiceMealDetailsActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.startPageForResult
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.vertical

/**
 *  选择服务分类
 */
class ChooseServicesActivity : BaseVMActivity<ChooseServicesViewModel, ActivityChooseServicesBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<ChooseServicesActivity>()
        }
    }

    private val data = arrayListOf<ChooseServiceBean>()
    private val leftCateAdapter = ChooseServiceLeftCateAdapter(data)

    private val rightData = arrayListOf<ChooseServiceBean.SecondCate>()
    private val rightCateAdapter = ChooseServiceRightCateAdapter(rightData)

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "服务分类"
        binding.titleBar.leftImageView().click { finish() }

        binding.rvCate.vertical().adapter = leftCateAdapter
        binding.rvList.vertical().adapter = rightCateAdapter

        leftCateAdapter.setOnItemClick { view, position, item ->
            if (item == null) return@setOnItemClick
            if (item.isSelect) return@setOnItemClick
            data.forEach {
                it.isSelect = it.id == item.id
            }
            leftCateAdapter.notifyDataSetChanged()
            rightData.clear()
            rightData.addAll(item.list?: mutableListOf())
            rightCateAdapter.notifyDataSetChanged()
        }
    }

    override fun initData() {
        vm.getAllSkillList()
    }

    override fun startObserve() {
        vm.getAllSkillListData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                data.clear()
                data.addAll(it?.data?: mutableListOf())
                if (data.isNotEmpty()) data[0].isSelect = true
                leftCateAdapter.notifyDataSetChanged()
                rightData.clear()
                rightData.addAll(data[0].list?: mutableListOf())
                rightCateAdapter.notifyDataSetChanged()
            }
        }
    }

    fun onServiceItemClick(item: ChooseServiceBean.SecondCate.ThirdCate?) {
        if (item?.id == 0) {
            toast("暂无服务")
            return
        }
        val topIsMeal = item?.meal_type
        if (topIsMeal == 1){
            startPageForResult<ServiceMealDetailsActivity>("ids" to item?.id, "topIsMeal" to topIsMeal){code, data ->
                if (code == 200 && data != null) {
                    data.putExtra("is_hourly_worker", item?.is_hourly_worker)
                    data.putExtra("topIsMeal", topIsMeal)
                    data.putExtra("cate_id", item?.cate_id.toString())
                    setResult(200, data)
                    finish()
                }
            }
        }else{
            startPageForResult<ChooseServiceDetailActivity>(
                "ids" to item?.id,
                "topIsMeal" to topIsMeal,
            ){code, data ->
                if (code == 200 && data != null) {
                    data.putExtra("is_hourly_worker", item?.is_hourly_worker)
                    data.putExtra("topIsMeal", topIsMeal)
                    data.putExtra("cate_id", item?.cate_id.toString())
                    setResult(200, data)
                    finish()
                }
            }
        }

    }

}